var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "services/shopping", "react", "maishu-wuzhui", "maishu-ui-toolkit", "maishu-wuzhui-helper", "react-dom", "maishu-dilu"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    const shopping_1 = require("services/shopping");
    const React = require("react");
    const wuzhui = require("maishu-wuzhui");
    const ui = require("maishu-ui-toolkit");
    const maishu_wuzhui_helper_1 = require("maishu-wuzhui-helper");
    const ReactDOM = require("react-dom");
    const maishu_dilu_1 = require("maishu-dilu");
    class RegionFreightDialog extends React.Component {
        constructor(props) {
            super(props);
            this.FREE_AMOUNT = "FreeAmount";
            this.FREIGHT = "Freight";
            this.state = {};
        }
        show() {
            ui.showDialog(this.element);
        }
        hide() {
            ui.hideDialog(this.element);
        }
        componentDidMount() {
            this.validator = new maishu_dilu_1.FormValidator(this.element);
        }
        render() {
            let dataItem = this.state.dataItem || {};
            let dataSource = this.props.dataSource;
            return (React.createElement("div", { className: "modal fade", ref: (e) => this.element = e || this.element },
                React.createElement("div", { className: "modal-dialog" },
                    React.createElement("div", { className: "modal-content" },
                        React.createElement("div", { className: "modal-header" },
                            React.createElement("button", { type: "button", className: "close", "data-dismiss": "modal" },
                                React.createElement("span", { "aria-hidden": "true" }, "\u00D7"),
                                React.createElement("span", { className: "sr-only" }, "Close")),
                            React.createElement("h4", { className: "modal-title" }, "\u5730\u533A\u8FD0\u8D39")),
                        React.createElement("div", { className: "modal-body form-horizontal" },
                            React.createElement("div", { className: "form-group" },
                                React.createElement("label", { className: "col-sm-2 control-label" }, dataItem.RegionName)),
                            React.createElement("div", { className: "form-group" },
                                React.createElement("label", { className: "col-sm-2 control-label" }, "\u8FD0\u8D39"),
                                React.createElement("div", { className: "col-sm-10" },
                                    React.createElement("input", { name: this.FREIGHT, type: "text", className: "form-control", placeholder: "\u8BF7\u8F93\u5165\u8FD0\u8D39\u91D1\u989D", ref: (o) => {
                                            if (!o)
                                                return;
                                            o.value = dataItem.Freight;
                                        }, onChange: (e) => {
                                            dataItem.Freight = Number.parseFloat(e.target.value);
                                        } }))),
                            React.createElement("div", { className: "form-group" },
                                React.createElement("label", { className: "col-sm-2 control-label" }, "\u514D\u8FD0\u8D39"),
                                React.createElement("div", { className: "col-sm-10" },
                                    React.createElement("input", { name: this.FREE_AMOUNT, type: "text", className: "form-control", placeholder: "\u8BF7\u8F93\u5165\u514D\u8FD0\u8D39\u91D1\u989D", ref: (o) => {
                                            if (!o)
                                                return;
                                            o.value = dataItem.FreeAmount;
                                        }, onChange: (e) => {
                                            dataItem.FreeAmount = Number.parseFloat(e.target.value);
                                        } }))),
                            React.createElement("div", { className: "modal-footer" },
                                React.createElement("button", { type: "button", className: "btn btn-default", "data-dismiss": "modal" }, "\u53D6\u6D88"),
                                React.createElement("button", { type: "button", className: "btn btn-primary", onClick: () => {
                                        dataSource.update(dataItem).then(() => {
                                            this.hide();
                                        });
                                    } }, "\u786E\u8BA4")))))));
        }
    }
    class FreightListPage extends React.Component {
        constructor(props) {
            super(props);
            this.state = { solutionId: this.props.data.solutionId, name: this.props.data.name };
            let shopping = this.props.createService(shopping_1.ShoppingService);
            this.dataSource = new wuzhui.DataSource({
                select: () => __awaiter(this, void 0, void 0, function* () {
                    let r = yield shopping.regionFreights(this.state.solutionId);
                    return { dataItems: r, totalRowCount: r.length };
                }),
                update: (dataItem) => shopping.updateRegionFreight(dataItem.Id, dataItem.Freight, dataItem.FreeAmount),
                insert: (dataItem) => shopping.addRegionFreight(dataItem.Freight, dataItem.FreeAmount),
                primaryKeys: ['Id']
            });
        }
        componentDidMount() {
            let self = this;
            maishu_wuzhui_helper_1.createGridView({
                dataSource: this.dataSource,
                columns: [
                    new wuzhui.BoundField({ dataField: 'RegionName', headerText: '地区' }),
                    new wuzhui.BoundField({
                        dataField: 'Freight', headerText: '运费', dataFormatString: '￥{C2}',
                        itemStyle: { textAlign: 'right' },
                    }),
                    new wuzhui.BoundField({
                        dataField: 'FreeAmount', headerText: '免运费金额', dataFormatString: '￥{C2}',
                        itemStyle: { textAlign: 'right' },
                    }),
                    new wuzhui.CustomField({
                        createItemCell(dataItem) {
                            let cell = new wuzhui.GridViewCell();
                            ReactDOM.render(React.createElement("a", { className: "btn btn-minier btn-info", style: { marginRight: 4 }, onClick: () => self.showDialog(dataItem) },
                                React.createElement("i", { className: "icon-pencil" })), cell.element);
                            return cell;
                        },
                        headerText: '操作',
                        headerStyle: { width: '80px' },
                        itemStyle: { textAlign: 'center' }
                    })
                ],
                element: this.freightListElement,
                pageSize: null
            });
        }
        componentDidUpdate() {
            this.dataSource.select();
        }
        showDialog(dataItem) {
            this.dialog.setState({ dataItem });
            this.dialog.show();
        }
        render() {
            let name = this.state.name;
            return React.createElement(React.Fragment, null,
                React.createElement("ul", { key: "tab", className: "nav nav-tabs" },
                    React.createElement("li", { className: "pull-left" },
                        React.createElement("h4", { style: { marginBottom: 0 } }, name)),
                    React.createElement("li", { className: "pull-right" },
                        React.createElement("button", { className: "btn btn-primary btn-sm", onClick: () => this.props.app.back() },
                            React.createElement("i", { className: "icon-reply" }),
                            React.createElement("span", null, "\u8FD4\u56DE")))),
                React.createElement("table", { key: "freights", ref: (e) => this.freightListElement = this.freightListElement || e }));
        }
    }
    exports.default = FreightListPage;
});
